package gpl.scotlandyard.android.beans.adapters;

import gpl.scotlandyard.android.activities.AbstractActivity;
import gpl.scotlandyard.beans.basics.Node;
import gpl.scotlandyard.services.PathFinder;

import java.util.List;

import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class FugitivePathsList extends ArrayAdapter<List<String>> implements PathFinder.Listener {

  private final AbstractActivity context;

  private final PathFinder pathFinder;

  public FugitivePathsList(AbstractActivity context, int resource, PathFinder pathFinder) {
    super(context, resource);
    this.context = context;
    this.pathFinder = pathFinder;
    pathFinder.addListener(this);
    notifyDataSetChanged();
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView = new TextView(context);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
    textView.setTypeface(Typeface.MONOSPACE);
    textView.setGravity(Gravity.LEFT);
    if (position == 0) {
      textView.setText(pathFinder.getLongestSafePathLength() + " safe turns, " + pathFinder.getSafePaths().size() + " choices.");
      return textView;
    }
    StringBuilder sb = new StringBuilder(50);
    for (Node node : pathFinder.getSafePaths().get(position - 1)) {
      sb.append(" ").append(String.format("%-3s", node));
    }
    textView.setText(sb);
    return textView;
  }

  @Override
  public int getCount() {
    return pathFinder.getSafePaths().size() + 1;
  }

  @Override
  public void updated() {
    notifyDataSetChanged();
  }

}
